package com.zhejiangzhengyuan.municipal_way.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhejiangzhengyuan.municipal_common.entity.MaintenanceRecord;
import com.zhejiangzhengyuan.municipal_common.entity.dto.FileDto;
import com.zhejiangzhengyuan.municipal_common.entity.dto.MaintenanceManageDto;
import com.zhejiangzhengyuan.municipal_common.entity.vo.MaintenanceManageVo;
import com.zhejiangzhengyuan.municipal_common.entity.vo.MaintenanceRecordFuzzySelectVo;
import com.zhejiangzhengyuan.municipal_common.mapper.MaintenanceRecordMapper;
import com.zhejiangzhengyuan.municipal_common.response.ResponseVo;
import com.zhejiangzhengyuan.municipal_common.service.MaintenanceRecordService;
import com.zhejiangzhengyuan.municipal_common.utils.ConstantUtil;
import com.zhejiangzhengyuan.municipal_common.utils.excelFileUtils.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/** @author lin_ju Create on 2019/12/3 */
@Api(tags = "养护记录管理")
@RestController
@Slf4j
@RequestMapping("/way/maintenanceRecord")
public class MaintenanceRecordController {

  @Autowired private MaintenanceRecordService maintenanceRecordService;
  @Autowired private MaintenanceRecordMapper maintenanceRecordMapper;

  @ApiOperation("养护管理 分页查询")
  @GetMapping("/selectAllMaintenanceRecord")
  public ResponseVo selectPageAll(@ApiParam("页码") Long page, @ApiParam("每页显示条数") Long limit) {
    page = Objects.isNull(page) || page <= 0 ? 1 : page;
    limit = Objects.isNull(limit) || limit <= 0 ? 14 : limit;
    Page<MaintenanceManageVo> maintenanceManagePage = new Page<>(page, limit);
    IPage<MaintenanceManageVo> maintenanceManageIPage =
        maintenanceRecordService.selectAllMaintenanceRecord(maintenanceManagePage);
    return maintenanceManageIPage == null
        ? new ResponseVo<>(ConstantUtil.ResultCode.SERVER_ERROR, "没有数据")
        : new ResponseVo<>(ConstantUtil.ResultCode.SUCCESS, maintenanceManageIPage);
  }

  @ApiOperation("养护管理 模糊查询")
  @GetMapping("/fuzzySelectByMaintenanceObject")
  public ResponseVo fuzzySelectByMaintenanceObject(String str) {
    List<MaintenanceRecordFuzzySelectVo> maintenanceRecordFuzzySelectVos =
        maintenanceRecordService.fuzzySelectByMaintenanceObject(str);
    return maintenanceRecordFuzzySelectVos == null
        ? new ResponseVo<>(ConstantUtil.ResultCode.CLIENT_PARAM_ERROR, null)
        : new ResponseVo<>(ConstantUtil.ResultCode.SUCCESS, maintenanceRecordFuzzySelectVos);
  }

  @ApiOperation("根据ID删除")
  @DeleteMapping("/removeMaintenanceById/{id}")
  public ResponseVo removeMaintenanceById(@PathVariable Long id) {
    return maintenanceRecordService.removeById(id) ? ResponseVo.SUCCESS : ResponseVo.SERVER_ERROR;
  }

  @ApiOperation("根据ID 批量删除 格式：'ids':[7,9,10]")
  @DeleteMapping("/removeMaintenanceByIds")
  public ResponseVo removeBridgeByIds(@RequestBody Map<String, List<Long>> map) {
    List<Long> idList = map.get("ids");
    return maintenanceRecordService.removeByIds(idList)
        ? ResponseVo.SUCCESS
        : ResponseVo.SERVER_ERROR;
  }

  @ApiOperation("新建记录")
  @PostMapping("/addMaintenance")
  public ResponseVo addMaintenance(
          @ModelAttribute MaintenanceRecord maintenanceRecord) {
    Long id= maintenanceRecordService.addMaintenance(maintenanceRecord);
    return id==null ?
            ResponseVo.CLIENT_PARAM_ERROR:
            new ResponseVo<>(ConstantUtil.ResultCode.SUCCESS, id);
  }

  @ApiOperation("编辑记录")
  @PostMapping("/updateMaintenance")
  public ResponseVo updateMaintenance(
          @ModelAttribute MaintenanceManageDto dto) {
    int i = maintenanceRecordService.updateMaintenance(dto);
    return i > 0 ? ResponseVo.SUCCESS : ResponseVo.CLIENT_PARAM_ERROR;
  }

  @ApiOperation("统计图")
  @GetMapping("/maintenanceRecordChart")
  public ResponseVo maintenanceRecordChart(String startTime, String endTime) {
    return maintenanceRecordService.maintenanceRecordChart(startTime, endTime);
  }

  @ApiOperation("导出数据")
  @GetMapping("/export")
  public void excelBridge(HttpServletResponse response) {
    List<MaintenanceManageVo> maintenanceManages =
        maintenanceRecordMapper.selectMaintenanceManageVo();
    HashSet<String> exclude = new HashSet<>();
    exclude.add("createTime");
    exclude.add("updateTime");
    ExcelUtil.objectToExcelAndDownload(
        response, exclude, "养护管理数据表", MaintenanceManageVo.class, maintenanceManages);
  }

  @ApiOperation("更新养护文件")
  @PostMapping("/updateMaintenanceRecordFile")
  public ResponseVo updateMaintenanceRecordFile(FileDto dto) {
    int id = maintenanceRecordService.updateMaintenanceRecordFile(dto);
    return id == 0
        ? ResponseVo.CLIENT_PARAM_ERROR
        : new ResponseVo<>(ConstantUtil.ResultCode.SUCCESS, id);
  }

  @ApiOperation("模糊查询")
  @GetMapping("/maintenanceFuzzy")
  public ResponseVo maintenanceFuzzy(
      String name,
      String userInfo,
      String startTime,
      String endTime,
      @ApiParam("页码") Long page,
      @ApiParam("每页显示条数") Long limit)
      throws ParseException {
    page = Objects.isNull(page) || page <= 0 ? 1 : page;
    limit = Objects.isNull(limit) || limit <= 0 ? 14 : limit;
    Page<MaintenanceManageVo> maintenanceManagePage = new Page<>(page, limit);
    Page<MaintenanceManageVo> maintenanceFuzzy =
        maintenanceRecordMapper.maintenanceFuzzy(
            maintenanceManagePage, name, userInfo, startTime, endTime);
    return maintenanceFuzzy == null
        ? ResponseVo.SERVER_ERROR
        : new ResponseVo<>(ConstantUtil.ResultCode.SUCCESS, maintenanceFuzzy);
  }
}
